<template>
  <zero-vxe-grid v-loading="loading">
    <div slot="header">
      <el-collapse-transition>
        <div v-show="viewModuleOptions.showSearch">
          <!--    查询条件-->
          <vxe-form ref="queryForm" :data="viewModuleOptions.searchForm" title-align="right">
            <vxe-form-item title="名称" field="name" :item-render="{}">
              <template #default="{ data }">
                <vxe-input max-length="50" v-model="data.name" showWordCount clearable placeholder="请输入名称"/>
              </template>
            </vxe-form-item>

            <vxe-form-item title="状态" field="status" :item-render="{}">
              <template #default="{ data }">
                <vxe-select v-model="data.status" placeholder="请选择状态" clearable>
                  <vxe-option v-for="dict in dict.type.enable_status" :key="dict.value" :label="dict.label"
                              :value="dict.value"></vxe-option>
                </vxe-select>
              </template>
            </vxe-form-item>

            <vxe-form-item title="创建时间" field="createTime" :item-render="{}">
              <template #default="{ data }">
                <el-date-picker v-model="data.createTime" style="width: 240px" value-format="yyyy-MM-dd"
                                type="daterange" range-separator="-" start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                :picker-options="viewModuleOptions.pickerOptions" :editable="false"/>
              </template>
            </vxe-form-item>
            <vxe-form-item>
              <vxe-button content="搜索" round @click="searchSubmit" status="primary"/>
              <vxe-button content="重置" round @click="searchResetForm"/>
            </vxe-form-item>
          </vxe-form>
        </div>
      </el-collapse-transition>
    </div>
    <div slot="main">
      <vxe-grid v-bind="viewModuleOptions" :ref="viewModuleOptions.ref" v-on="viewModuleOptions.fns">
        <template #buttons>
          <vxe-button content="新增" status="primary" icon="vxe-icon-add"
                      @click="initDialog({ref:addUpdateDialogRef})"
                      v-hasPermi="['sysRole:add']"/>
          <vxe-button content="批量删除" status="danger" icon="vxe-icon-delete" @click="handleDelete"
                      v-hasPermi="['sysRole:delete']" :disabled="viewModuleOptions.dataListSelections.length <= 0"/>
          <vxe-button content="导出" status="success" icon="vxe-icon-cloud-download"
                      @click="handleExport({ref:importDialogRef,title:'系统角色'})" v-hasPermi="['sysRole:export']"/>
          <vxe-button content="导入" icon="vxe-icon-cloud-upload" v-hasPermi="['sysRole:import']"
                      @click="initDialog({ref:importDialogRef})"/>
        </template>

        <template #operation="{ row }">
          <vxe-button mode="text" status="primary" content="分配权限" v-hasPermi="['sysRole:assignPermissions']"
                      @click="initDialog({ref:treeDialogRef,row:row,title:'分配权限'})"></vxe-button>
          <vxe-button mode="text" status="primary" content="修改" v-hasPermi="['sysRole:update']"
                      @click="initDialog({ref:addUpdateDialogRef,row:row})"></vxe-button>
          <vxe-button mode="text" status="danger" content="删除" v-hasPermi="['sysRole:delete']"
                      @click="handleDelete(row)"></vxe-button>
        </template>

        <template #status="{ row }">
          <vxe-switch v-model="row.status" open-label="启用" open-value="T" close-label="禁用" close-value="F"
                      v-if="checkPermi(['sysRole:updateStatus'])"
                      @change="statusChange(row)"/>
          <dict-tag v-else :options="dict.type.enable_status" :value="row.status"/>

        </template>
        <template #tools>
          <vxe-button :title="viewModuleOptions.showSearch ? '隐藏搜索' : '显示搜索'" @click="showOrHideClick"
                      icon="vxe-icon-search" circle/>
          <vxe-button :title="viewModuleOptions.isFullscreen ? '还原' : '全屏'" @click="toggleIsFullscreen"
                      class="tool-btn"
                      :icon="viewModuleOptions.isFullscreen ? 'vxe-icon-minimize' : 'vxe-icon-fullscreen'" circle/>
        </template>
      </vxe-grid>

      <!-- 添加、修改 -->
      <add-update :ref="addUpdateDialogRef"
                  :getDataList="getDataList"
                  :dictType="dict.type"/>

      <!-- 导入、导出 -->
      <ZeroImportExport :ref="importDialogRef"
                        :searchForm="viewModuleOptions.searchForm"
                        :importUrl="viewModuleOptions.importUrl"
                        :importTemplateUrl="viewModuleOptions.importTemplateUrl"
                        :exportUrl="viewModuleOptions.exportUrl"
                        :getDataList="getDataList"/>
      <!-- 分配权限 -->
      <power-tree :ref="treeDialogRef"/>
    </div>
  </zero-vxe-grid>
</template>

<script>

import viewModule from '@/mixins/vxe-grid.js'; // 公共js
import addUpdate from './components/add-update.vue' // 添加、修改模块
import powerTree from './components/power-tree.vue'; // 权限树
import {checkPermi} from "@/utils/permission.js";

export default {
  name: "sysRole",
  components: {powerTree, addUpdate},
  dicts: ['enable_status', 'data_scope'],
  mixins: [viewModule],
  data() {
    return {
      addUpdateDialogRef: 'addUpdateDialogRef',
      importDialogRef: 'importDialogRef',
      treeDialogRef: 'treeDialogRef',
      viewModuleOptions: {
        dataUrl: '/sysRole/pageList',
        removeUrl: '/sysRole',
        exportUrl: '/sysRole/export',
        importUrl: '/sysRole/import',
        importTemplateUrl: '/sysRole/importTemplate',
        //列动态定义
        columns: [
          {type: 'checkbox', width: 60},
          {field: 'name', title: '名称', resizable: true},
          {field: 'perms', title: '权限标识符', resizable: true},
          {field: 'status', title: '状态', sortable: true, resizable: true, slots: {default: 'status'}},
          {field: 'remarks', title: '备注', showOverflow: true, resizable: true},
          {field: 'createTime', title: '创建时间', sortable: true, resizable: true},
          {field: 'updateTime', title: '修改时间', sortable: true, resizable: true},
          {
            title: '操作',
            width: 200,
            slots: {default: 'operation'},
            visible: checkPermi(['sysRole:update', 'sysRole:delete', 'sysConfig:assignPermissions'])
          }
        ]
      }
    };
  },
  watch: {
    //时间筛选特殊处理，重置createTime时也将根据这个字段衍生出来的字段置为null
    'viewModuleOptions.searchForm.createTime': function (val) {
      this.viewModuleOptions.searchForm.startCreateTime = val ? val[0] : null
      this.viewModuleOptions.searchForm.endCreateTime = val ? val[1] : null
    }
  },
  computed: {},
  methods: {
    checkPermi,
    //状态列表中切换
    async statusChange(row) {
      let text = row.status === "T" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"角色吗？')
          .then(() => {
            this.loading = true
            let config = {
              url: '/sysRole/updateStatus',
              method: 'put',
              params: {id: row.id, status: row.status}
            }
            this.$axiosAuth(config)
                .catch(() => {
                  row.status = row.status === "T" ? "F" : "T";
                }).finally(() => {
              this.getDataList();
            });
          }).catch(() => {
      }).finally(() => {
        row.status = row.status === "T" ? "F" : "T";
      });
    },
  }
};
</script>
